package start.doubudouyin.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    public static void main(String[] args) {
       String str="123456";
       System.out.println(Encrypt(str));
       String passwordinput="123456";
       System.out.println(isVaild(passwordinput,Encrypt(str)));
    }
    public static String Encrypt(String str){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(str.getBytes());
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = String.format("%02x", b);
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 算法不可用", e);
        }
    }
    public static boolean isVaild(String cin_str,String original_str){
       return Encrypt(cin_str).equals(original_str);
    }
}
